package service.impl;

import dao.EmployeeDao;
import entity.Employee;
import org.apache.ibatis.session.SqlSession;
import service.EmployeeService;
import util.MyBatisUtil;

import java.sql.SQLException;
import java.util.List;

public class EmployeeServiceImpl  implements EmployeeService {
    SqlSession sqlSession = MyBatisUtil.createSqlSession();
    @Override
    public Employee findEmployee(String employeeNo, String password) throws SQLException {
        Employee employee = sqlSession.getMapper(EmployeeDao.class).findEmployee(employeeNo, password);
        MyBatisUtil.closeSqlSession(sqlSession);
        return employee;
    }

    @Override
    public List<Employee> getEmployeesByPage(String name, int pageNo, int pageSize) throws SQLException {
        List<Employee> list = sqlSession.getMapper(EmployeeDao.class).getEmployeesByPage(name, pageNo,pageSize);
        MyBatisUtil.closeSqlSession(sqlSession);
        return list;
    }

    @Override
    public int getEmployeesCount(String name) throws SQLException {
        int count = sqlSession.getMapper(EmployeeDao.class).getEmployeesCount(name);
        MyBatisUtil.closeSqlSession(sqlSession);
        return count;
    }

    @Override
    public int updateEmployee(Employee employee) throws SQLException {
        int count = sqlSession.getMapper(EmployeeDao.class).updateEmployee(employee);
        MyBatisUtil.closeSqlSession(sqlSession);
        return count;
    }

    @Override
    public int addNewEmployee(Employee employee) throws SQLException {
        int count = sqlSession.getMapper(EmployeeDao.class).addNewEmployee(employee);
        MyBatisUtil.closeSqlSession(sqlSession);
        return count;
    }

    @Override
    public Employee getEmployeeById(int id) throws SQLException {
        Employee employee = sqlSession.getMapper(EmployeeDao.class).getEmployeeById(id);
        MyBatisUtil.closeSqlSession(sqlSession);
        return employee;
    }

    @Override
    public int getEmployeeByNo(String employeeNo) throws SQLException {
        int count = sqlSession.getMapper(EmployeeDao.class).getEmployeeByNo(employeeNo);
        MyBatisUtil.closeSqlSession(sqlSession);
        return count;
    }

    @Override
    public int deleteEmployeeById(int id) throws SQLException {
        int count = sqlSession.getMapper(EmployeeDao.class).deleteEmployeeById(id);
        MyBatisUtil.closeSqlSession(sqlSession);
        return count;
    }

    @Override
    public int updatePasswordById(int id, String pwd) throws SQLException {
        int count = sqlSession.getMapper(EmployeeDao.class).updatePasswordById(id,pwd);
        MyBatisUtil.closeSqlSession(sqlSession);
        return count;
    }
}
